package com.cc.user.controller;

import java.util.Arrays;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cc.common.utils.ResponseData;
import com.cc.common.utils.ResultMsg;
import com.cc.user.entity.UserEntity;
import com.cc.user.vo.ReceiveAddressVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.cc.user.entity.ReceiveAddressEntity;
import com.cc.user.service.ReceiveAddressService;
import com.cc.common.utils.R;

import javax.servlet.http.HttpSession;


/**
 * 
 *
 * @author leifengyang
 * @email leifengyang@gmail.com
 * @date 2020-12-11 11:30:58
 */
@RestController
@RequestMapping("user/receiveaddress")
@Api(tags="收货地址")
public class ReceiveAddressController {
    @Autowired
    private ReceiveAddressService receiveAddressService;


    @PostMapping("/updatereceiveaddr")
    @ApiOperation("根据收货地址id修改收货地址")
    public ResponseData getAllReceiveAddr(ReceiveAddressVo receiveAddressVo) {
        //封装添加收货地址类ReceiveAddressEntity
        ReceiveAddressEntity receiveAddressEntity = new ReceiveAddressEntity();
        receiveAddressEntity.setReceiverName(receiveAddressVo.getReceiverName());
        receiveAddressEntity.setProvince(receiveAddressVo.getCity());
        receiveAddressEntity.setCity(receiveAddressVo.getCity());
        receiveAddressEntity.setRegion(receiveAddressVo.getRegion());
        receiveAddressEntity.setDetailAddress(receiveAddressVo.getDetailAddress());
        receiveAddressEntity.setReceiverMobile(receiveAddressVo.getReceiverMobile());
        boolean flag = receiveAddressService.update(receiveAddressEntity, new QueryWrapper<ReceiveAddressEntity>().eq("id", receiveAddressVo.getId()));
        if (!flag){
            return ResponseData.success("修改失败",false);
        }
        return ResponseData.success("修改成功",true);
    }

    @PostMapping("/getallreceiveaddr")
    @ApiOperation("获取所有收货地址")
    public ResponseData<List<ReceiveAddressEntity>> getAllReceiveAddr(HttpSession session) {
        UserEntity user = (UserEntity) session.getAttribute("loginuser");
        Long uid = user.getUid();

        //通过uid获取用户的所有收货地址
        List<ReceiveAddressEntity> allReceiveAddr = receiveAddressService.getallReceiveAddr(uid);
        return ResponseData.success(allReceiveAddr);
    }

    @PostMapping("/deletereceiveaddr")
    @ApiOperation("删除单条收货地址")
    @ApiImplicitParam(name = "id",value = "收货地址id",paramType = "query",dataType = "Long",required = true)
    public ResponseData deleteOneReceiveAddr(@RequestParam("id")Long id) {
        //通过收货地址id删除收货地址
        int flag = receiveAddressService.deleteOneReceiveAddr(id);
        if (flag <= 0){
            return ResponseData.success("删除失败",false);
        }
        return ResponseData.success("删除成功",true);
    }

    @PostMapping("/deleteMutiReceiveAddr")
    @ApiOperation("删除多条收货地址")
    public ResponseData deleteMutiReceiveAddr(@RequestParam("id_list")List<Long> id_list) {
        //通过收货地址id删除收货地址
        int flag = receiveAddressService.deleteMutiReceiveAddr(id_list);
        if (flag <= 0){
            return ResponseData.success("删除失败",false);
        }
        return ResponseData.success("删除成功",true);
    }


    @PostMapping("/addreceiveaddr")
    @ApiOperation("增加收货地址")
    public ResponseData addReceiveAddr(ReceiveAddressVo receiveAddressVo, HttpSession session) {
        UserEntity user = (UserEntity) session.getAttribute("loginuser");
        Long uid = user.getUid();
        //封装添加收货地址类ReceiveAddressEntity
        ReceiveAddressEntity receiveAddressEntity = new ReceiveAddressEntity();
        receiveAddressEntity.setUid(uid);
        receiveAddressEntity.setReceiverName(receiveAddressVo.getReceiverName());
        receiveAddressEntity.setProvince(receiveAddressVo.getCity());
        receiveAddressEntity.setCity(receiveAddressVo.getCity());
        receiveAddressEntity.setRegion(receiveAddressVo.getRegion());
        receiveAddressEntity.setDetailAddress(receiveAddressVo.getDetailAddress());
        receiveAddressEntity.setReceiverMobile(receiveAddressVo.getReceiverMobile());
        int flag = receiveAddressService.addReceiveAddr(receiveAddressEntity);

        ResultMsg resultMsg = new ResultMsg();
        if (flag <= 0){
            return ResponseData.success("添加地址失败",false);
        }
        return ResponseData.success("添加地址成功",true);
    }

    /**
     * 列表
     */


    /**
     * 信息
     */
    @GetMapping("/info/{id}")
    public R info(@PathVariable("id") Long id){
		ReceiveAddressEntity receiveAddress = receiveAddressService.getById(id);
        return R.ok().put("receiveAddress", receiveAddress);
    }
//
//    /**
//     * 保存
//     */
//    @PostMapping("/save")
//    public R save(@RequestBody ReceiveAddressEntity receiveAddress){
//		receiveAddressService.save(receiveAddress);
//        return R.ok();
//    }
//
//    /**
//     * 修改
//     */
//    @PostMapping("/update")
//    public R update(@RequestBody ReceiveAddressEntity receiveAddress){
//		receiveAddressService.updateById(receiveAddress);
//        return R.ok();
//    }
//
//    /**
//     * 删除
//     */
//    @PostMapping("/delete")
//    public R delete(@RequestBody Long[] ids){
//		receiveAddressService.removeByIds(Arrays.asList(ids));
//        return R.ok();
//    }

}
